package com.tools.box.util.localpermiss

import android.content.Context
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions

class LocalPermissUtils {
    companion object {

        fun showPremissDialog(context: Context,premissListener: PremissListener) {
         var isFirst = localSharedPreUtils.getInstance(context).getBoolean("isFirstWritePremiss",false)


            if (isFirst){
                premissListener.onSuccess()
                return
            }
            val permissDialog = LocaPremissDialog(context, R.style.dialog)
            permissDialog.show()
            permissDialog.setOnDialogClickListener(object : LocaPremissDialog.OnDialogClickListener {
                override fun onAgree() {
                    setReadPremiss(context,premissListener)
                }

                override fun onCancel() {
                    premissListener.onFail()
                }
            })
        }

        private fun setReadPremiss(context: Context,premissListener: PremissListener){
            XXPermissions.with(context)
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                        localSharedPreUtils.getInstance(context).putBoolean("isFirstWritePremiss",true)
                        premissListener.onSuccess()
                    }

                    override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                        super.onDenied(permissions, never)
                        premissListener.onFail()
                    }
                })

        }

        interface PremissListener{
            fun onSuccess()
            fun onFail()
        }

    }
}